<?php
header("Content-Type: text/html; charset=iso-8859-1");
require_once 'include.php';

class arbitrage{
	var $id;
	var $fields;	
	function arbitrage($id = "",$field = 'no',$saison = ""){
		$fields = describe("arbitrages");
		$q = "select * from arbitrages where $field = '$id'";
		$r = ExecRequete($q);
		if($s = LigneSuivante($r)){
			foreach($fields as $field => $type){
				$this->$field = $s->$field;
			}
		}
		else{
			foreach($fields as $field_id => $type){
				$this->$field_id = ($field_id == $field) ? $id : "";
			}			
		}
	}	
	function get_salle(){
		if($this->salle){
			$salle = new salle($this->salle);
			return $salle;
		}
		else return "";
	}
	
	function update(){
		$no_jours = array("di","lu","ma","me","je","ve","sa");
		$fields = describe('arbitrages');
		if($this->no || $this->id){
			$q = "replace into arbitrages set ";
			$tab_date = explode("-",$this->date);
			$this->jour = strtoupper($no_jours[date("w",mktime(0, 0, 0, $tab_date[1]  , $tab_date[2], $tab_date[0]))]);
			foreach($fields as $field => $type){
				$q .= $field." = '".addslashes($this->$field)."', ";
			}
			$q = substr($q,0,-2);			
		}
		$this->adversaire = (strstr(strtolower($this->local),"lutry") !== false) ? $this->visiteur : $this->local;
		if($r = ExecRequete($q)) return mysql_affected_rows();
		else return false;
	}
	
	function form_edit($arbitrages_row_id = "",$all = ""){
		global $jours;
		$fields = describe('arbitrages');
		if($this->no) echo "<h2>Modifier l'arbitrage $this->no</h2>";
		else echo "<h2>Nouvel arbitrage</h2>";
		echo "<form action = 'update_arbitrage_admin.php' method = 'post' name = 'form_arbitrage' id = 'form_arbitrage'/>\n";
		foreach($fields as $field => $type){
			echo leftCol($field." : ",30);
			if($field == 'id'){
				$value = ($this->no) ? $this->id : "";
				$echo = "<input type = 'hidden' name = 'id' value = '$value' />";
				$echo .= ($this->no) ? $this->id : "nouveau";
				echo rightCol($echo,69);
			}
			elseif($field == 'jour'){
				$echo = "<input type = 'hidden' name = 'jour' value = '$this->jour' /><span id = 'print_jour'>".$jours[strtolower($this->jour)]."</span>";
				echo rightCol($echo,69);
			}
			elseif($field == 'salle'){
				$echo = "<div style = 'position: relative'>";
				$salle = new salle($this->salle);
				$value = "[$salle->no] $salle->nom";
				$echo .= "<input type = 'text' size = '35' name = 'salle' id = 'salle' value = '$value'/>";
				$echo .= "<span id='indicator1_salle' style='display: none'><img src='images/spinner.gif' alt='recherche...' /></span>";
				$autocomplete = "<div id='autocomplete_choices_salles' class='autocomplete'></div>";
				$echo .= "<input type = 'hidden' name = 'salle_id' id = 'salle_id' value = '$this->salle' />";
				$echo .= "</div>";
				echo rightCol($echo,69);				
			}
			elseif($field == 'date'){
				$date_array = explode("-",$this->date);
				$jour = (count($date_array) == 3) ? $date_array[2] : "";
				$mois = (count($date_array) == 3) ? $date_array[1] : "";
				$annee = (count($date_array) == 3) ? $date_array[0] : "";
				$echo = "<input type = 'text' name = 'djour' id = 'djour' value = '$jour' size = '2' maxlength = '2' onblur = \"writediv(print_jour_from_date(document.getElementById('annee').value,document.getElementById('mois').value,document.getElementById('djour').value),'print_jour')\" onkeyup = \"if(this.value.length == 2) document.form_arbitrage.mois.focus()\"/>.";
				$echo .= "<input type = 'text' name = 'mois' id = 'mois' value = '$mois' size = '2' maxlength = '2' onkeyup = \"if(this.value.length == 2) document.form_arbitrage.annee.focus()\" onblur = \"writediv(print_jour_from_date(document.getElementById('annee').value,document.getElementById('mois').value,document.getElementById('djour').value),'print_jour')\"/>.";
				$echo .= "<input type = 'text' name = 'annee' id = 'annee' value = '$annee' size = '4' maxlength = '4' onblur = \"writediv(print_jour_from_date(document.getElementById('annee').value,document.getElementById('mois').value,document.getElementById('djour').value),'print_jour')\"/>";
				$echo  .= "<input type = 'hidden' name = 'date' value = '$this->date' />";
				echo rightCol($echo,69);
			}
			elseif($field == 'heure'){
				$heure_array = explode(":",$this->heure);
				$heure = (count($heure_array)) ? $heure_array[0] : "";
				$minute = (count($heure_array)) ? $heure_array[1] : "";
				$seconde = (count($heure_array) == 3) ? $heure_array[2] : 0;
				$echo = "<input type = 'text' name = 'heures' value = '$heure' size = '2' maxlength = '2' onkeyup = \"if(this.value.length == 2) document.form_arbitrage.minute.focus()\" />:";
				$echo .= "<input type = 'text' name = 'minute' value = '$minute' size = '2' maxlength = '2' />";
				$echo .= "<input type = 'hidden' name = 'seconde' value = '$seconde' />";
				$echo  .= "<input type = 'hidden' name = 'heure' value = '$this->heure' />";
				echo rightCol($echo,69);

			}
			elseif($field == 'arbitre1'){
				$echo = "<div style = 'position: relative'>";
				$arbitre = new membre($this->arbitre1);
				$value = ($this->arbitre1) ? "$arbitre->prenom $arbitre->nom" : "";
				$echo .= "<input type = 'text' size = '20' name = 'arbitre1' id = 'arbitre1' value = '$value'/>";
				$echo .= "<span id='indicator1_arbitre1' style='display: none'><img src='images/spinner.gif' alt='recherche...' /></span>";
				$autocomplete .= "<div id='autocomplete_choices_arbitre1' class='autocomplete'></div>";
				$echo .= "<input type = 'hidden' name = 'arbitre1_id' id = 'arbitre1_id' value = '$this->arbitre1' />";
				$echo .= "</div>";
				echo rightCol($echo,69);
			}
			elseif($field == 'arbitre2'){
				$echo = "<div style = 'position: relative'>";
				$arbitre = new membre($this->arbitre2);
				$value = ($this->arbitre2) ? "$arbitre->prenom $arbitre->nom" : "";
				$echo .= "<input type = 'text' size = '20' name = 'arbitre2' id = 'arbitre2' value = '$value'/>";
				$echo .= "<span id='indicator1_arbitre2' style='display: none'><img src='images/spinner.gif' alt='recherche...' /></span>";
				$autocomplete .= "<div id='autocomplete_choices_arbitre2' class='autocomplete'></div>";
				$echo .= "<input type = 'hidden' name = 'arbitre2_id' id = 'arbitre2_id' value = '$this->arbitre2' />";
				$echo .= "</div>";
				echo rightCol($echo,69);
			}
			elseif($field == 'commentaire'){
				$echo = "<div style = 'position: relative'><textarea name = 'commentaire' cols = '20' rows = '3'>$this->commentaire</textarea></div>";
				echo rightCol($echo,69);
			}
			else echo rightCol("<input type = 'text' name = '$field' value = '".$this->$field."'  onblur = 'this.value = this.value.toUpperCase()'/>",69);
		}
		echo "<br class = 'clear'/>";
		$text = ($this->no) ? "modifier" : "nouveau";
		echo "<p class = 'center'><input type = 'hidden' name = 'table_row_id' value = 'arbitrages_row_id' /><input type = 'hidden' name = 'all' value = '1' /><button type = 'button' name =  'action' value = 'edit' onclick = 'update_arbitrage()'>$text</button><button onclick = 'writediv(\"\",\"center_box\");center_box()' type = 'button' >annuler</button></p></form>\n";
		if($this->date >= date('Y-m-d')){
			echo "<h2>autres arbitres disponibles</h2>\n";
			$bonnes = array();
			$q = "select distinct(equipe) from matchs where date = '$this->date' and salle = '$this->salle'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$equipe = new equipe($s->equipe,"nom");
				$bonnes[$equipe->id] = $s->equipe;
			}
			$q = "select equipe_id,nom from entrainements left join equipes on equipe_id = equipes.id where jour = '$this->jour' and salle = '$this->salle'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$bonnes[$s->equipe_id] = $s->nom;
			}
			$equipes = liste('equipes');	
			$q = "select distinct(equipe) from matchs where date = '$this->date' and salle != '$this->salle'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				if(isset($equipes)){
					foreach($equipes as $id => $nom){
						if($nom == $s->equipe){unset($equipes[$id]); }
					}			
				}
			}
			if(isset($equipes)){
				$q = "select equipe_id,nom from entrainements left join equipes on equipe_id = equipes.id where jour = '$this->jour' and salle != '$this->salle'";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					if(isset($equipes)){
						foreach($equipes as $id => $nom){
							if($nom == $s->nom){unset($equipes[$id]); }
						}			
					}
				}
			}
			$categories = array("Bonnes disponibilit&eacute;s" => "bonnes", "Disponibles" => "equipes");
			foreach($categories as $titre => $table){
				echo "<p class = 'bold center'>$titre</p>";
				$i = 0;
				${"print_membres_".$table}= array();
				if(isset($$table)){
					foreach($$table as $id => $nom){
						$equipe = new equipe($id);
						$membres = $equipe->list_membres();
						foreach($membres as $membre_id => $membre){
							$ok = (($membre->arbitre) || (strstr($this->ligue,"M16") !== FALSE && $membre->arbitre_junior)) ? 1 : 0;
							if($ok){

								foreach($membre->arbitrages as $marq_id => $marq_no){
									$arbitrage = new arbitrage($marq_no,"no");
									if($arbitrage->date == $this->date) $ok = 0;
								}
								foreach($membre->marquages as $marq_id => $marq_no){
									$marquage = new match($marq_no);
									if($marquage->date == $this->date) $ok = 0;
								}
							}
							if($ok){
								$junior = ($membre->arbitre_junior && !$membre->arbitre) ? "[J]&nbsp;" : "";
								${"print_membres_".$table}[$membre->id] = $junior.$membre->prenom." ".$membre->nom." ($membre->nb_arbitrages)";
								if($membre->nb_arbitrages == "0" || $membre->nb_arbitrages == "1") ${"print_membres_".$table}[$membre->id] = "<span class = 'highlight'>".${"print_membres_".$table}[$membre->id]."</span>";
							}
						}
					}
					asort(${"print_membres_".$table});
					foreach(${"print_membres_".$table} as $membre_id => $print){
						if($table == 'bonnes' || !array_key_exists($membre_id,$print_membres_bonnes)){
							$print .= 
							$echo = "<span style = 'cursor: pointer' onclick = 'show_membre($membre_id,\"$this->no\")'>$print</span>";
							if(++$i % 2) echo leftCol($echo);
							else echo rightCol($echo);					
						}
					}
					if(!count(${"print_membres_".$table})) echo "<p class = 'center fade'>aucune</p>\n";
					if($i % 2) echo rightCol("");
					echo "<br class = 'clear' />";
				}
			}		
		}
		elseif($this->no) echo "<p class = 'center fade'>La date de l'arbitrage est pass&eacute;e</p>\n";
		echo $autocomplete;
	}
	
	function delete(){
		if($this->no){
			$q = "delete from arbitrages where no = '$this->no' limit 1";
			return (ExecRequete($q)) ? 1 : 0;
		}
		else return 0;
	}
}
?>
